<template>
  <div>
    <a-layout>
      <a-layout-content style="height: calc(100vh - 88px); padding: 2px">
        <VxeBasicTable ref="TableRef" v-bind="gridOptions">
          <template #toolbar_buttons>
            <TableConfig.ToolBtn
              :filter-call="handleOpenFilter"
              :create-call="handleEdit"
              :delete-call="handleDelete"
              :show-export="false"
              :show-import="false"
              :show-delete="false"
            />
          </template>
          <template #action="{ row }">
            <TableConfig.Action
              :record="row"
              @edit="handleEdit"
              @delete="handleDelete"
            />
          </template>
        </VxeBasicTable>
      </a-layout-content>
    </a-layout>
    <OrgEdit
      v-model:visible="showEdit"
      :data="current"
      :orglist="treeData"
      @done="reload"
    />
  </div>
</template>

<script lang="ts" setup>
  import { ref, reactive } from 'vue';
  import { message } from 'ant-design-vue/es';
  import {
    BasicTableProps,
    VxeBasicTable,
    VxeGridInstance
  } from '@/components/VxeTable';
  import { messageLoading } from 'ele-admin-pro/es';
  import {
    listOrganizations,
    removeOrganization
  } from '@/api/system/organization';
  import OrgEdit from './components/org-edit.vue';
  import { TableConfig } from './config';
  import Columns from './config/vxeColumn';
  import type { Organization } from '@/api/system/organization/model';

  const TableRef = ref<VxeGridInstance>();
  const ifSHowFilter = ref(false);
  const gridOptions = reactive<BasicTableProps>({
    border: true,
    height: 'auto',
    stripe: false,
    columnConfig: {
      resizable: true
    },
    treeConfig: {
      rowField: 'id',
      parentField: 'pid',
      iconOpen: 'vxe-icon-square-minus',
      iconClose: 'vxe-icon-square-plus'
    },
    columns: Columns,
    pagerConfig: {
      enabled: false
    },
    proxyConfig: {
      ajax: {
        query: async () => {
          const data = await listOrganizations();
          treeData.value = data;
          return data;
        }
      }
    }
  });

  const treeData = ref<Organization[]>([]);

  // 当前编辑数据
  const current = ref<Organization | null>(null);

  // 是否显示编辑弹窗
  const showEdit = ref(false);

  /* 刷新表格 */
  const reload = () => {
    TableRef.value?.commitProxy('query');
  };

  /* 打开编辑弹窗 */
  const handleEdit = (row?: Organization | null) => {
    current.value = row ?? null;
    showEdit.value = true;
  };

  const handleOpenFilter = () => {
    ifSHowFilter.value = true;
  };

  /* 删除单个 */
  const handleDelete = (row: Organization) => {
    const hide = messageLoading('请求中..', 0);
    removeOrganization(row.id)
      .then((msg) => {
        hide();
        message.success(msg);
        reload();
      })
      .catch((e) => {
        hide();
        message.error(e.message);
      });
  };
</script>

<script lang="ts">
  export default {
    name: 'SystemOrg'
  };
</script>
